#! /bin/bash

# ------------------------------------------
# check_account users 30
# 
# Created by Statemood, 2013.08.11 19:50
# Updated by Statemood, 2013.09.28 10.35
#            Statemood@gmail.com
#
# Project monitor:
#    https://github.com/Statemood/monitor
# ------------------------------------------

monfile="/etc/passwd"
exceptsh=':/sbin/nologin$|:/bin/false$|:/bin/sync$|:/sbin/shutdown$|:/sbin/halt$'
     key="$2"
      ok=0
 warning=1
critical=2
 unkonwn=3

case "$1" in
	users)
		usercount=`cat $monfile | wc -l`
		if [ "$usercount" = "$key" ]
		then
		    echo "OK - Total $usercount user accounts"
			exit 0
		else
			echo "CRITICAL - Total $usercount user accounts"
			exit 2
		fi
		;;
	shell)
		shellcount=`egrep -v "$exceptsh" $monfile | wc -l`
		if [ "$shellcount" = "$key" ]
		then
			echo "OK - Total $shellcount users has a valid Shell"
			exit 0
		else
			echo "CRITICAL - Total $shellcount users has a valid Shell"
			exit 2
		fi
		;;
	guid)
		guidcount=`awk -F ':' '{print $3,$4}' $monfile | egrep '^0 | 0$' | wc -l`
		if [ "$guidcount" = "$key" ]
		then
			echo "OK - Total $guidcount users's UID/GID is 0"
			exit 0
		else
			echo "CRITICAL -  Total $guidcount users's UID/GID is 0"
			exit 2
		fi
		;;
	sudo)
        sudocount=`grep -v '^#' /etc/sudoers | egrep 'ALL=|NOPASSWD'     | wc -l`
		if [ "$sudocount" = "$key" ]
		then
			echo "OK - Total $sudocount users have permission to access sudo"
			exit 0
		else
			echo "CRITICAL -  Total $sudocount users have permission to access sudo"
			exit 2
		fi
		;;
esac
